# 预设菜单
import bpy
from bpy.types import Operator, Menu, Panel
from bl_operators.presets import AddPresetBase


class OBJECT_MT_display_presets(Menu):
    bl_label = "预设"
    preset_subdir = "object/display"
    preset_operator = "script.execute_preset"
    draw = Menu.draw_preset


class AddPresetObjectDisplay(AddPresetBase, Operator):
    '''Add a Object Display Preset'''
    bl_idname = "camera.object_display_preset_add"
    bl_label = "Add Object Display Preset"
    preset_menu = "OBJECT_MT_display_presets"

    # 用于存储预设值的变量 <exec>  variable used for all preset values
    preset_defines = ["obj = bpy.context.object"]

    # 存储在预设中的属性列表 <eval> properties to store in the preset
    preset_values = ["obj.display_type",
                     "obj.show_bounds",
                     "obj.display_bounds_type",
                     "obj.show_name",
                     "obj.show_axis",
                     "obj.show_wire"]

    # 存储预设的位置
    # Mac: /用户/用户名/资源库/Application Support/Blender/2.92/scripts/presets/object/display
    # Win: C:/Users/用户名/....../2.92/scripts/presets/object/display
    preset_subdir = "object/display"


class My_PT_Panel(Panel):
    bl_label = 'My Panel'
    bl_idname = 'MY_PT_PANEL'
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = 'object'

    def draw_header_preset(self, context):
        r = self.layout.row(align=True)
        r.menu(menu=OBJECT_MT_display_presets.__name__,
               text=OBJECT_MT_display_presets.bl_label,
               icon="PREFERENCES")
        r.operator(AddPresetObjectDisplay.bl_idname, text="", icon='ZOOM_IN')
        r.operator(AddPresetObjectDisplay.bl_idname, text="", icon='ZOOM_OUT').remove_active = True

    def draw(self, context):
        self.layout.label(text="hello Blender")


classes = (OBJECT_MT_display_presets,
           My_PT_Panel,
           AddPresetObjectDisplay)


def register():
    for cls in classes:
        bpy.utils.register_class(cls)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)


if __name__ == "__main__":
    register()
